<?php

namespace App\Http\Controllers\Api;

use App\Models\UserDeposit;
use App\Models\UserDepositMethod;
use Illuminate\Http\Request;

class DepositController extends ApiBaseController
{
    /**
     * @param Request $request
     * @param UserDeposit $model
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ApiException
     */
    public function deposit(Request $request, UserDeposit $model)
    {
        $data = $request->all();
        if ($vr = $this->verifyField($data, [
            'method_id' => 'bail|required|integer', //充值方式id
            'amount' => 'bail|required|numeric', //金额 USD
            'image' => 'bail|nullable', //充值凭证
        ])) return $vr;

        $data['uid'] = $this->getUid($request);
        $result = $model->deposit($data);
        return $this->success($result);
    }

    /**
     * 手工充值模式，上传充值截图用
     * @param Request $request
     * @param UserDeposit $model
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function capture(Request $request)
    {
        $data = $request->all();
        if ($vr = $this->verifyField($data, [
            'order_id' => 'bail|required', //充值订单
            'image' => 'bail|required', //充值成功截图
        ])) return $vr;

        $data['uid'] = $this->getUid($request);
        $flag = UserDeposit::storeCapture($data);

        return $flag ? $this->success() : $this->failure();
    }

    public function history(Request $request, UserDeposit $model)
    {
        $data = $request->all();
        if ($vr = $this->verifyField($data, [
            'page' => 'required|integer', //充值订单
        ])) return $vr;
        $data['uid'] = $this->getUid($request);
        return $this->success($model->history($data));
    }

    public function method(Request $request)
    {
        return $this->success(UserDepositMethod::methodList());
    }


    public function prepare()
    {

    }

    public function detail(Request $request, UserDeposit $model)
    {
        $data = $request->all();
        if ($vr = $this->verifyField($data, [
            'id' => 'bail|required|integer', //id
        ])) return $vr;

        $data['uid'] = $this->getUid($request);
        $result = $model->detail($data);
        return $this->success($result);
    }
}
